package com.demo.framework;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;

import com.demo.app.CrashHandler;

/**
 * 
 * @ClassName: FrameworkApplication
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 谢云腾
 * @date 2013-3-25 下午03:46:29
 * @version 1.0
 */
public class FrameworkApplication extends Application {

    public static FrameworkApplication instance;

    // 管理所有的 Activity
    private List<Activity> activityList = new LinkedList<Activity>();

    public static FrameworkApplication getInstance() {
        return (FrameworkApplication) instance;
    }

    @Override
    public void onCreate() {
        // 注册crashHandler
        instance = this;
        try {
            CrashHandler crashHandler = CrashHandler.getInstance();
            crashHandler.init(getApplicationContext());
        }
        catch(Throwable e) {
            // TODO: handle exception
            System.out.println(e.toString());
        }

        super.onCreate();
    }

    /**
     * 
     * @Title: addActivity
     * @Description: TODO(添加Activity到容器中 )
     * @param @param activity
     * @return void 返回类型
     * @throws
     */
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    /**
     * 
     * @Title: finishActivity
     * @Description: TODO(关闭某个 activity)
     * @param @param finishActivity
     * @return void 返回类型
     * @throws
     */
    public void finishActivity(Activity finishActivity) {
        for (int i = 0; i < activityList.size(); i++) {
            if (activityList.get(i) == finishActivity) {
                activityList.remove(i);
                finishActivity.finish();
            }
        }
    }

    /**
     * 
     * @Title: exit
     * @Description: TODO(遍历所有Activity并finish)
     * @param
     * @return void 返回类型
     * @throws
     */
    public void exit() {
        for (Activity act : activityList) {
            if (!act.isFinishing()) {
                act.finish();
            }
        }
        System.exit(0);
    }

}
